package com.example.ccj.b.c.ui.install;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.ccj.R;
import com.example.ccj.b.c.dialog.DialogBase;
import com.example.ccj.b.c.dialog.DialogButtonOnClickListener;
import com.example.ccj.u.KeyboardUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;

/**
 * 添加一个选项dialog
 */
public class AddOneOptionDialog extends DialogBase {

    IAddOptionListener addOptionListener;
    EditText addOption;

    public AddOneOptionDialog(@NonNull Context context,IAddOptionListener addOptionListener) {
        super(context);
        this.addOptionListener = addOptionListener;
    }

    @Override
    public void onInit() {
        setLayout(R.layout.dialog_add_one_option);
        setTitleCenterText("添加选项");

        addOption = findViewById(R.id.dialog_add_one_option_text);

        setButtonSureText("确定").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultData();
            }
        });

        setButtonCancelText("取消").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        //绑定软键盘确定按钮
        addOption.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    resultData();
                    //隐藏软键盘
                    KeyboardUtils.hideSoftInput(addOption);
                    return true;
                }
                return false;
            }
        });

        //输入框获取焦点
        addOption.requestFocus();
    }


    private void resultData(){
        String string = addOption.getText().toString();
        if (!StringUtils.isEmpty(string)) {
            if (addOptionListener != null) addOptionListener.onResult(addOption.getText().toString(),null);
            dismiss();
        } else {
            ToastUtils.showShort("未输入选项名称");
        }
    }

}
